<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
      <el-form-item label="钻机编号" prop="borerId">
        <el-input
          v-model="queryParams.borerId"
          placeholder="请输入钻机编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:post:add']"
        >新增</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="borerList">
      <el-table-column type="index" label="序号" align="center" />
      <el-table-column label="钻机编号" align="center" prop="borerId" />
      <el-table-column label="钻机型号" align="center" prop="borerNum" />
      <el-table-column label="钻机型式" align="center" prop="borerType" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:post:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:post:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="钻机编号" prop="borerId">
          <el-input v-model="form.borerId" placeholder="请输入钻机编号" />
        </el-form-item>
        <el-form-item label="钻机型号" prop="borerNum">
          <el-input v-model="form.borerNum" placeholder="请输入钻机型号" />
        </el-form-item>
        <el-form-item label="钻机型式" prop="borerType">
          <el-select v-model="form.borerType" placeholder="请选择钻机型式" clearable :style="{width: '100%'}">
            <el-option v-for="(item, index) in borerTypepion" :key="index" :label="item.label"
                       :value="item.value" :disabled="item.disabled">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="placeExplain">
          <el-input v-model="form.borerExplain" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>

import { delInf, listInf, addInf, updateInf, getborer } from '@/api/new/rig_inf'

export default {
  name: "Place",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      inWay:0,
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      borerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        borerId: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        borerId:[
          { required: true, message: "钻机编号不能为空", trigger: "blur" }
        ],
        borerNum: [
          { required: true, message: "钻机型号不能为空", trigger: "blur" }
        ],
        borerType: [
          { required: true, message: "钻机型式不能为空", trigger: "blur" }
        ],
        borerExplain:[]
      },
      borerTypepion:[{
        label: '岩心钻机',
        value: '岩心钻机'
      }, {
        label: '旋转式钻机',
        value: '旋转式钻机'
      }, {
        label: '锚杆钻机',
        value: '锚杆钻机'
      }]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      listInf(this.queryParams).then(response => {
        this.borerList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.inWay=0;
      this.open = true;
      this.title = "新增钻机";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.inWay=1;
      const Id = row.borerId;
      getborer(Id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改钻机";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.inWay==1) {
            updateInf(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInf(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const Id = row.borerId;
      this.$modal.confirm('是否确认删除编号为"' + Id + '"的数据项？').then(function() {
        return delInf(row);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
  }
};
</script>
